package com.sondon.mayi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.adapter.CartListAdapter;
import com.sondon.mayi.adapter.CartList_ItemChanageListener;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.Cart;
import com.sondon.mayi.entity.CartListEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : CartActivity
 * @Author : 蔡文锋
 * @DateTime：2015年6月9日 下午10:44:02
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { 购物车 }
 */
@EActivity(R.layout.cart)
public class CartActivity extends BaseActivity {
	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	ListView cart_list;//购物车列表
	@ViewById
	TextView cart_price;//总价
	@ViewById
	CheckBox choose_radio;//全选
	@ViewById
	TextView go_play;//结算
	@ViewById
	RelativeLayout cart_empty_layout;//购物车为空
	@Bean
	CartListAdapter adapter;//适配器
	
	@SuppressLint("UseSparseArrays")
	private HashMap<Integer, Double> itemCheckedMap=new HashMap<Integer,Double>();
	
	private double cart_total=0.00;
	@AfterViews
	public void initView() {

		topBar.setTopBarClickListener(new TopBarClickListener() {
			@Override
			public void rightBtnClick() {
			}
			@Override
			public void leftBtnClick() {
				onBackPressed();
			}
		});
		//设置适配器
		cart_list.setAdapter(adapter);
		
		//查询购物车列表请求
		AsyncHttpUtil.get(URLConfigs.MyCart_ukey+myprefs.Ukey().get(), null, responseHandler);
		
		//设置购物车列表改变NUM监听事件
		adapter.SetListener(new CartList_ItemChanageListener() {
			
			//改变item的状态:  是否被选择  数量
			@Override
			public void ItemNumChanage(double item_total,int position,double num,boolean checked) {
				LogUtil.e(TAG, "ItemNumChanage  :"+item_total);
				cart_total=cart_total+item_total;
				//设置总价
				cart_price.setText("￥ :"+cart_total);
				//记录item被选择的数量,第几个
				if(checked){
					itemCheckedMap.put(position, num);
				}else{
					itemCheckedMap.remove(position);
				}
				
				//设置结算
				go_play.setText("结算("+itemCheckedMap.size()+")");
				if(itemCheckedMap.size()==adapter.getCount()){
					choose_radio.setChecked(true);
				}
			}
			
			//item被删除
			@Override
			public void ItemDelChanage(int position) {
				
				//更新itemCheckedMap
				if(itemCheckedMap.containsKey(position)){
					itemCheckedMap.remove(position);
				}
				
				//更新listview
				adapter.GetData().remove(position);
				adapter.notifyDataSetChanged();
			}
		});
		
		//全选监听事件
		choose_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					adapter.SetAllChecked(isChecked);
			}
		});
		
		
	}

	@Click({ R.id.go_play,R.id.cart_empty_layout})
	public void onClick(View v) {
		Bundle bundle=new Bundle();
		switch (v.getId()) {
		case R.id.go_play://结算
			if(itemCheckedMap.size()==0){
				ToastUtils.show(context, "请选择要购买的商品!");
				return ;
			}
			//整理订单数据
			ArrayList<Cart> list=new ArrayList<Cart>();
			Iterator<Integer> keys=itemCheckedMap.keySet().iterator();
			while(keys.hasNext()){
				
				Integer key=keys.next();
				Cart cart=adapter.GetData().get(key);
				cart.setNum(""+itemCheckedMap.get(key));
				list.add(cart);
				
				LogUtil.e(TAG, "position :"+key +"  num :"+itemCheckedMap.get(key));
			}
			
			
			bundle.putSerializable("cartlist_item_checked", list);
			openActivity(OrderConfirmActivity_.class,bundle);
			break;
			
		case R.id.cart_empty_layout:
//			bundle.putInt("position", 0);//跳转到商品页面
//			openActivity(MainTabActivity_.class,bundle);
			myprefs.position().put(0);
			openActivity(MainTabActivity_.class);
			break;
		default:
			break;
		}
	}
	
	
	//查询购物车列表请求响应
	TextHttpResponseHandler responseHandler=new TextHttpResponseHandler() {
		
		@Override
		public void onSuccess(int statue, Header[] arg1, String json) {
			LogUtil.e(TAG, "mycart  json:"+json);
			if(statue==200){
				CartListEntity cartentity=GsonUtil.GetFromJson(json, CartListEntity.class);
				if(cartentity.isStatus()){
					//设置全选
					choose_radio.setChecked(true);
					//支付可以点击
					go_play.setEnabled(true);
					//设置支付按钮背景为cat_price
					go_play.setBackgroundColor(getResources().getColor(R.color.cat_price));
					
					ArrayList<Cart> list=cartentity.getList();
					//设置适配器
					adapter.SetData(list);
					
					cart_empty_layout.setVisibility(View.GONE);
					cart_list.setVisibility(View.VISIBLE);
				}else{
//					ToastUtils.show(context, cartentity.getMsg());
					//设置不全选
					choose_radio.setChecked(false);
					//支付可以不点击
					go_play.setEnabled(false);
					//设置支付按钮背景为gray
					go_play.setBackgroundColor(getResources().getColor(R.color.gray));
					
					cart_empty_layout.setVisibility(View.VISIBLE);
					cart_list.setVisibility(View.GONE);
				}
			}
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
			ToastUtils.show(context, R.string.network_unavailable);
		}
	};

}
